<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/23 0023
 * Time: 15:15
 */

namespace  app\mobile\controller;
use think\Db;
use think\Controller;


class News extends Bases
{

    public function _initialize(){
        if(!session('id')){
            $this->redirect('/admin/index/index');
        }
    }

    //客照列表
    public function index(){
        header("Cache-control: private");
        $is_recom=input('param.is_recom');
        $nclass_id=input('param.nclass_id');
        //推荐状态搜索
        if(isset($is_recom) && $is_recom !==''){
                $where['w.is_recom'] = $is_recom;
        }
        if(isset($nclass_id) && $nclass_id !==''){
            $where['w.nclass_id'] = $nclass_id;
        }
       $where['w.is_del'] = 0;
        $where['w.sign'] = "mobile";
        if($this->request->isPost()){

        }
       $data['list'] = Db::name('worknews')
           ->alias('w')
           ->join('newsclass n','n.nclass_id = w.nclass_id')
           ->field('w.id,w.title,w.main_img,w.main_img,w.content,w.is_recom,w.create_time,w.ps_time,
                w.click,w.sign,w.is_show,w.seo_title,w.seo_keywords,w.works_photo,w.seo_description,w.sort,w.is_del,n.nclass_id,n.name')
//           ->where('w.is_del=0 and w.is_show=1 and sign="pc"')
//           ->field("*,CEIL(FROM_UNIXTIME(w.ps_time,'%d')/7) zhou")
//           ->field("*,CEIL(FROM_UNIXTIME(w.ps_time,'%d')/7) zhou")
           ->where($where)
           ->select();
//        foreach ($data as $v){
//            $data['zhou']=ceil(date($v['ps_time'],'%d')/7);
//        }
        $info=Db::name('newsclass')->where('show',1)->order('nclass_id asc')->select();
        $data['info']=$info;
//       var_dump($data);die;
        $this->assign('data',$data);
        return $this->fetch();
    }

    //按钮状态
    public function start(){
//        $is_recom = (int)input('post.is_recom');
        $id = (int)input('post.id');
        if($id){
            $info=Db::name('worknews')->where('id',$id)->find();
            if ($info['is_recom']==1){
                $data['is_recom']=0;
            }else{
                $data['is_recom']=1;
            }
            $result = Db::name('worknews')->where('id',$id)->update($data);
            if($result){
                return json(['code'=>1,'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            return json(['code'=>1,'msg'=>'数据异常']);
        }
    }
    //分类管理
    public function vclass(){
//            $where['id']=0;
//            $where['sign']="pc";
        $data = Db::name('newsclass')->where('show',1)->order('nclass_id desc')->select();
        $this->assign('data',$data);

        return $this->fetch();
    }

    public function add_vclass(){
        $id= intval(input('param.id'));
        if(request()->isAjax()){
            $data=[
                'name'=>input('param.name'),
                'show'=>intval(input('param.show'))
            ];
            $id= intval(input('param.id'));
            if($id> 0){
                $result = Db::name('newsclass')->where('class_id',$id)->update($data);
            }else{
                $result = Db::name('newsclass')->insert($data);
            }
            if($result !==false){
                return json(['code'=>1,'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }

        }
        if($id){
            $data = Db::name('newsclass')->where('class_id',$id)->find();
            $this->assign('data',$data);
        }
        return $this->fetch();
    }


    //添加客照
    public function add_works()
    {

        $imgids_1 = Db::name('worknews')->field('works_photo')->select();
        $imgids_2 = Db::name('zp')->field('works_photo')->select();
        $imgids_3 = Db::name('hxzp')->field('works_photo')->select();
        $ids = "";
        foreach ($imgids_1 as $v) {
            $ids .= $v['works_photo'] . ',';
        }
        foreach ($imgids_2 as $v) {
            $ids .= $v['works_photo'] . ',';
        }
        foreach ($imgids_3 as $v) {
            $ids .= $v['works_photo'] . ',';
        }
        $id_x = Db::name('works_pic')->where('pic_id', 'in', $ids)->select();
        if (!is_dir('./uploads/worknews/')) mkdir('./uploads/worknews/'); // 如果不存在则创建
        //清除不存在的图片
//        $dir = "./uploads/worknews/";  //要获取的目录
//        //先判断指定的路径是不是一个文件夹
//        if (is_dir($dir)) {
//            if ($dh = opendir($dir)) {
//                while (($file = readdir($dh)) !== false) {
//                    //文件名的全路径 包含文件名
//                    $filePath = $dir . $file;
//                    if (is_dir($filePath)) {
//                        if ($dh_2 = opendir($filePath)) {
//                            while (($file_2 = readdir($dh_2)) != false) {
//                                $filePath_2 = $filePath . '/' . $file_2;
//                                if (strpos($filePath_2, '.jpg')) {
//                                    $filePath_3[] = $filePath_2;
//                                }
//                            }
//                            closedir($dh_2);
//                        }
//                    }
//                }
//                closedir($dh);
//            }
//        }
//        $ext=[];
//        foreach ($id_x as $k => $value) {
//            $ext[$k]='.'.str_replace('\\', '/', $value['works_pic']);
//        }
//        if (!empty($filePath_3)){
//            foreach ($filePath_3 as $v) {
//                if(!in_array($v, $ext)) {
//                    unlink($v);
//                }
//            }
//        }
        $info = Db::name('newsclass')->where('show',1)->select();
        $this->assign('info',$info);
        return $this->fetch();
    }
    //编辑
    public function edit_works(){
        $id = input('param.id');
        if($id){
            $data =Db::name('worknews')->alias('w')
                ->alias('w')
                ->join('newsclass n','n.nclass_id = w.nclass_id')
                ->field('w.id,w.title,w.main_img,w.main_img,w.content,w.is_recom,w.create_time,w.ps_time,
                w.click,w.sign,w.is_show,w.is_show,w.seo_title,w.seo_keywords,w.works_photo,w.seo_description,w.sort,w.is_del,n.nclass_id,n.name')
                ->where('id',$id)->find();
            $data['info']=Db::name('newsclass')->where('show',1)->select();
            $works_pic = Db('works_pic')->where('pic_id','in',$data['works_photo'])->field('works_pic,pic_id')->select();
            $data['content'] = html_entity_decode($data['content']);
            $this->assign('data',$data);
            $this->assign('imgurl',$works_pic);
        }
        return $this->fetch();
    }
    //确认提交
    public function addworks(){
        $id = input('post.id');
        $array_works_pic= isset($_POST['works_pic'])?implode(',',$_POST['works_pic']):'';
        $data=[
            'title'=>input('post.title'),
            'ps_time'=>strtotime(input('post.ps_time')),
            'nclass_id'=>intval(input('post.nclass_id')),
            'week'=>intval(input('post.week')),
            'is_show'=>intval(input('post.is_show')),
            'is_recom'=>intval(input('post.is_recom')),
            'sort'=>intval(input('post.sort')),
            'works_photo'=>$array_works_pic,
            'content'=>input('post.content'),
            'click'=>intval(input('post.click')),
            'seo_title'=>input('param.seo_title'),
            'seo_keywords'=>input('param.seo_keywords'),
            'seo_description'=>input('param.seo_description'),
            'sign'=>"mobile"
        ];
        //检查时间
        if($data['ps_time']){

            $date_now=date('j',$data['ps_time']); //得到是几号
            $cal_result=ceil($date_now/7); //计算是第几个星期几
            $month = date('Y-m',$data['ps_time']);//得到是几月
            if($id){
                $sql ="SELECT CEIL(FROM_UNIXTIME(ps_time,'%d')/7) zhou FROM  `dh_worknews`  WHERE FROM_UNIXTIME(ps_time,'%Y-%m')='$month' and id != $id and sign='mobile'";
            }else{
                $sql ="SELECT CEIL(FROM_UNIXTIME(ps_time,'%d')/7) zhou FROM  `dh_worknews`  WHERE FROM_UNIXTIME(ps_time,'%Y-%m')='$month' and sign='mobile'";
            }
            $week = Db::name('worknews')->query($sql);
            $week2= array();
            foreach ($week  as $k=>$v){
                foreach ($v as $m => $n) {
                    $week2[] = $n;
                }
            }
//            if(in_array($cal_result,$week2)){
//                return json(['code'=>0,'msg'=>"错误信息：".date("Y年m月",$data['ps_time'])."的第".$cal_result."周的拍摄时间已存在"]);
//            }

        }
        $file_main_img = request()->file('main_img');
        if($file_main_img){
            $file_info_main_img = $file_main_img->move('./uploads/worknews/');
            if($file_info_main_img){
                $data['main_img'] = '/uploads/worknews/'.$file_info_main_img->getSaveName();
            }else{
                return json(['code'=>0,'msg'=>$file_main_img->getError()]);
            }
        }
        if($id){
            if($file_main_img){
                $old_img = Db::name('worknews')->where('id',$id)->value('main_img');
                if($old_img){
                    unlink('.'.str_replace('\\','/',$old_img));
                }
            }
            $data['update_time']=time();
            $result = DB::name('worknews')->where('id',$id)->update($data);
        }else{
            $data['create_time']=time();
            $result = Db::name('worknews')->insert($data);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }
    //删除
    public function worksdel(){
        $id = intval(input('post.id'));
        if($id){
            $res = Db::name('worknews')->where('id',$id)->update(['is_del'=>1]);;
        }
        if($res){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }
    //改变排序
    public function ChangeSort(){
        $sort = (int)input('post.sort');
        $id = (int)input('post.id');
        if($id){
            $data['sort']=$sort;
            $result = Db::name('worknews')->where('id',$id)->update($data);
            if($result){
                return json(['code'=>1,'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            return json(['code'=>1,'msg'=>'数据异常']);
        }

    }

    //多图上传
    public function addworksajax(){
        $work_pic = request()->file('file');
        if(isset($work_pic)){
            $work_pic_file_info = $work_pic->move('./uploads/worknews/');
            if ($work_pic_file_info) {
                $data['works_pic'] = '/uploads/worknews/'.$work_pic_file_info->getSaveName();
                if($data){
                    $result = Db::name("works_pic")->insert($data);
                    $id = Db::name('works_pic')->getLastInsID();
                    $re = [
                        'status' => 200,
                        'info' => $id ,
                        'fileName'=>$data['works_pic'],
                    ];
                    if($result){
                        return json($re);
                    }
                }
            }
        }
    }
    //移除多图
    public function delworksajax(){
        $id = intval(input('param.id'));
        if($id>0){
            $url = Db::name('works_pic')
                ->where('pic_id',$id)
                ->value('works_pic');
            if($url){
                unlink(str_replace('\\','/',$_SERVER['DOCUMENT_ROOT'].$url));

            }
            $result = Db::name("works_pic")
                ->where('pic_id',$id)
                ->delete();
            if($result){
                return json(['status' => 200, 'info' => '移除成功']);
            }else{
                return  json(['status' => 400, 'info' => '移除失败']);
            }
        }else{
            return json(['status' => 400, 'info' => '数据异常']);
        }
    }

    public function hx(){
        $where['is_del']=0;
        $where['sign']="mobile";
        $data = Db::name('hxzp')->where($where)->order('sort desc,id desc')->select();
        $this->assign('data',$data);

        return $this->fetch();
    }
    public function add_hx(){

        return $this->fetch();
    }
    public function edit_hx(){
        $id = input('param.id');
        if($id){
            $data =Db::name('hxzp')->where('id',$id)->find();
            $works_pic = Db('works_pic')->where('pic_id','in',$data['works_photo'])->field('works_pic,pic_id')->select();
            $data['content'] = html_entity_decode($data['content']);
            $this->assign('data',$data);
            $this->assign('imgurl',$works_pic);
        }
        return $this->fetch();
    }
    //确认提交
    public function addhx(){
        $id = input('post.id');
        $array_works_pic= isset($_POST['works_pic'])?implode(',',$_POST['works_pic']):'';
        $data=[
            'title'=>input('post.title'),
            'ps_time'=>strtotime(intval(input('post.ps_time'))),
            'is_show'=>intval(input('post.is_show')),
            'sign'=>"mobile",
            'sort'=>intval(input('post.sort')),
            'works_photo'=>$array_works_pic,
            'content'=>input('post.content'),
            'click'=>intval(input('post.click')),
            'seo_title'=>input('param.seo_title'),
            'seo_keywords'=>input('param.seo_keywords'),
            'seo_description'=>input('param.seo_description'),
        ];
        $file_main_img = request()->file('main_img');
        if($file_main_img){
            $file_info_main_img = $file_main_img->move('./uploads/worknews/');
            if($file_info_main_img){
                $data['main_img'] = '/uploads/worknews/'.$file_info_main_img->getSaveName();
            }else{
                return json(['code'=>0,'msg'=>$file_main_img->getError()]);
            }
        }
        if($id){
            if($file_main_img){
                $old_img = Db::name('hxzp')->where('id',$id)->value('main_img');
                if($old_img){

                    unlink('.'.str_replace('\\','/',$old_img));
                }
            }
            $data['update_time']=time();
            $result = DB::name('hxzp')->where('id',$id)->update($data);
        }else{
            $data['create_time']=time();
            $result = Db::name('hxzp')->insert($data);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }

    //删除
    public function hxdel(){
        $id = intval(input('post.id'));
        if($id){
            $res = Db::name('hxzp')->where('id',$id)->update(['is_del'=>1]);;
        }
        if($res){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }


    //改变排序
    public function ChangehxSort(){
        $sort = (int)input('post.sort');
        $id = (int)input('post.id');
        if($id){
            $data['sort']=$sort;
            $result = Db::name('hxzp')->where('id',$id)->update($data);
            if($result){
                return json(['code'=>1,'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            return json(['code'=>1,'msg'=>'数据异常']);
        }

    }

}